let app = getApp()
Component({
    properties: {
        pagetype: {
          type:String
        }
      },
      data:{
        auth_show:false
      },
    lifetimes: {
        attached: function () {
              this.startInter()
              this.get_common()
         }
      },
    methods: {
      getPhoneNumber: function (e) {
        var that = this;
        if (e.detail.errMsg == "getPhoneNumber:ok") {
          wx.request({
            url: app.globalData.siteroot,
            data: {
              phonedata: e.detail.encryptedData,
              iv: e.detail.iv,
              userid:wx.getStorageSync('user_id'),
              do:'user_getPhone'
            },
            success: function (res) {
               that.setData({
                auth_show:false
               })
            }
          })
        }
      },
      get_common(){
        var that=this
        wx.request({
          url: app.globalData.siteroot,
          data: {
            do:'index_common'
          },
          success: function(res) {
            that.setData({
                base_css:res.data.info.styles.common
             })
             app.globalData.styles=res.data.info.styles.common
          var movies = [];
          var o = {}
          o=res.data.info.styles.common
          for (var item in o){
              var key = item;
              var value = o[key];
              var css='';
              if(typeof(o[key].height)!='undefined')
              {
                var h=o[key].height.value;
                h= h.toFixed(2);
                css=css+'height:'+h+'px;';
              }
              if(typeof(o[key].width)!='undefined')
              {
                var w=o[key].width.value;
                w= w.toFixed(2);
                css=css+'width:'+w+'px;';
              }
              if(typeof(o[key].top)!='undefined')
              {
                var t=o[key].top.value;
                t= t.toFixed(2);
                css=css+'top:'+t+'px;';
              }
              if(typeof(o[key].left)!='undefined')
              {
                var l=o[key].left.value;
                l= l.toFixed(2);
                css=css+'left:'+l+'px;';
              }
              if(typeof(o[key].transform)!='undefined')
              {
                css=css+'transform:'+o[key].transform.value+';';
              }
              if(typeof(o[key]['-webkit-transform'])!='undefined')
              {
                css=css+'-webkit-transform:'+o[key]['-webkit-transform'].value+';';
              }
              if(typeof(o[key].bd)!='undefined')
              {
                if(typeof(o[key].bd.t)!='undefined'&&typeof(o[key].bd.t.style)!='none')
                {
                  css=css+'border-top:'+o[key].bd.t.width+'px '+o[key].bd.t.style+' '+o[key].bd.t.color+';';
                }
                if(typeof(o[key].bd.r)!='undefined'&&typeof(o[key].bd.r.style)!='none')
                {
                  css=css+'border-right:'+o[key].bd.r.width+'px '+o[key].bd.r.style+' '+o[key].bd.r.color+';';
                }
                if(typeof(o[key].bd.b)!='undefined'&&typeof(o[key].bd.b.style)!='none')
                {
                  css=css+'border-bottom:'+o[key].bd.b.width+'px '+o[key].bd.b.style+' '+o[key].bd.b.color+';';
                }
                if(typeof(o[key].bd.l)!='undefined'&&typeof(o[key].bd.l.style)!='none')
                {
                  css=css+'border-left:'+o[key].bd.l.width+'px '+o[key].bd.l.style+' '+o[key].bd.l.color+';';
                }
              }
              if(typeof(o[key].font)!='undefined')
              {
                css=css+"family:"+o[key].font.family+';font-size:'+o[key].font.size+";color:"+o[key].font.color+";text-align:"+o[key].font.align+";";
                if(o[key].font.isB)
                {
                  css=css+"font-weight:bolder;";
                }
                if(o[key].font.isI)
                {
                  css=css+"font-style:italic;";
                }
              }
              if(typeof(o[key].bg)!='undefined'){
                    if(o[key].bg.type=='color')
                    {
                      css=css+"background-color:"+o[key].bg.color.color+';opacity:'+o[key].bg.color.alpha+";";
                    }
                    if(o[key].bg.image!='')
                    {
                      css=css+"background-image:url("+o[key].bg.image.path+");background-repeat:"+o[key].bg.image.repeat.repeat+";background-size:"+o[key].bg.image.repeat.size+";";
                    }
              }
              if(typeof(o[key].br)!='undefined'){
                 css=css+"border-radius:"+o[key].br.radius+"px;"
              }
              if(typeof(o[key].sd)!='undefined'){
                css=css+"box-shadow:"+o[key].sd.sd_width+"px "+o[key].sd.sd_blur+"px "+o[key].sd.sd_angle+"px "+o[key].sd.color+";";
              }
              if(typeof(o[key].image)!='undefined'){
                css=css+"background-image:url("+o[key].image.path+");background-repeat:"+o[key].image.repeat.repeat+";background-size:"+o[key].image.repeat.size+";";
              }
              o[key]=css;
            }
            that.setData({
              sty:o
            })
              if(that.data.pagetype=='login')
              {
               that.setData({
                    auth_item_id:'login_auth_block',
                    auth_item:res.data.info.common['login_auth']
                 })
              }
              if(that.data.pagetype=='phone')
              {
                  that.setData({
                    auth_item_id:'phone_auth_block',
                    auth_item:res.data.info.common['phone_auth'],
                 })
              }
              that.setData({
                is_member_info_author:wx.getStorageSync('site_config').is_member_info_author,
                is_phone_author:wx.getStorageSync('site_config').is_phone_author,
              })
          }
        })
      },
      startInter : function(){
        var that = this;
        var user_info=wx.getStorageSync('userinfo');
        that.data.inter= setInterval(
            function () {
              if(that.data.pagetype=='login')
              {
                if (user_info.length>0) {
                  that.setData({
                    auth_show:false
                })
                }
                else
                {
                  that.setData({
                    auth_show:true
                  })
               }
            }
            if(that.data.pagetype=='phone')
            {
             if(wx.getStorageSync('userinfo')!=''){
              wx.request({
                url: app.globalData.siteroot,
                data: {
                  do:'Member_getUserCenter',
                  user_id:wx.getStorageSync('user_id')
                },
                success: function(res) {
                  user_info=res.data.data.member_data.user_info;
                  wx.setStorageSync('userinfo',res.data.data.member_data.user_info);
                }
              })
              var user=wx.getStorageSync('userinfo')
              if(user.phone != null)
              {
                that.setData({
                  auth_show:false
                })
              }
              else
              {
                that.setData({
                  auth_show:true
                })
              }
             }
          }
            }, 5000);    
      },
      getUserProfile:function(e){
        var that=this
        wx.getUserProfile({
          desc: '用于完善会员资料',
          success: (res) => {
            that.setData({
              userInfo:res.userInfo,
              auth_show:false
            })
            app.globalData.userInfo=res.userInfo
            var userInfo=res.userInfo
            wx.login({
              success(res) {
                if (res.code) {
                  wx.request({
                    url: app.globalData.siteroot,
                    data: {
                      do:'user_login',
                      code:res.code
                    },
                    success: function(res) {
                      var userid=res.data.info.userid
                      wx.request({
                        url: app.globalData.siteroot,
                         data: {
                           do:'user_update',
                           userid:userid,
                           image:userInfo.avatarUrl,
                           nick:userInfo.nickName,
                           gender:userInfo.gender
                        },
                       success: function(res) {
                          that.triggerEvent ('changeuser');
                       }
                      })
                      that.setData({
                        pagetype:'phone'
                      })
                      var authInfo = {
                        show:res.data.info.copyright && 0 !=res.data.info.copyright.copyright_type,
                        pic_show:res.data.info.copyright && 1 ==res.data.info.copyright.copyright_type,
                        pic:res.data.info.copyright &&res.data.info.copyright.copyright_logo,
                        text_show:res.data.info.copyright && 2 == res.data.info.copyright.copyright_type,
                        text:res.data.info.copyright &&res.data.info.copyright.copyright,
                        is_pay_password: 0, 
                        distribution_id: res.data.info.distribution_id,
                        cart_count: res.data.info.cart_count,
                       distributor_id: res.data.info.distributor_id,
                       is_show_cart: res.data.info.is_show_cart,
                       is_show_sales_volume: res.data.info.is_show_sales_volume,
                       is_show_customer_service: res.data.info.is_show_customer_service,
                       is_member_info_author: res.data.info.is_member_info_author,
                       is_phone_author: res.data.info.is_phone_author,
                       customer_service_type: res.data.info.customer_service_type,
                      customer_service_url: res.data.info.customer_service_url,
                      cart_icon: res.data.info.cart_icon,
                      is_home_advert: res.data.info.is_home_advert,
                      is_open_store: res.data.info.is_open_store
                        }
                      that.get_common()
                      wx.setStorageSync('authInfo',authInfo);
                      wx.setStorageSync('staff_bind_id',res.data.info.staff_bind_id);
                      wx.setStorageSync('userinfo',res.data.info);
                      wx.setStorageSync('user_id',res.data.info.userid);
                    }
                  })
                } 
              }
            })
          
          }
        })
      },

      
      button_cancel:function(){
           this.setData({
                auth_show:false
           })
      }
    }
});
